package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.mapper.ProductSpecMapper;
import com.spzx.product.service.ProductSpecService;
import com.spzx.product.api.domain.ProductSpec;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, ProductSpec>
        implements ProductSpecService {
   /* @Override
    public Page<ProductSpec> getSpecList(ProductSpec productSpec) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<ProductSpec> iPage = new Page<>(pageDomain.getPageNum(),pageDomain.getPageSize());

        QueryWrapper<ProductSpec> queryWrapper = new QueryWrapper<>();
        String specName = productSpec.getSpecName();
        Long categoryId = productSpec.getCategoryId();
        queryWrapper.eq(categoryId!=null,"category_id",categoryId)
                .eq(specName!=null,"spec_name",specName).eq("ps.del_flag",productSpec.getDelFlag());
        List<ProductSpec> list = baseMapper.getSpecList(iPage,queryWrapper);
        iPage.setRecords(list);
        return iPage;
    }*/

    @Override
    public List<ProductSpec> getSpecList(ProductSpec productSpec) {
        return baseMapper.getSpecList(productSpec);
    }

    @Override
    public void checkSpecUnique(ProductSpec productSpec) {
        Long id = productSpec.getId();
        if (id == null) {
            productSpec.setCreateBy(SecurityUtils.getUsername());
            Long count = baseMapper.selectCount(new LambdaQueryWrapper<ProductSpec>().eq(ProductSpec::getCategoryId, productSpec.getCategoryId())
                    .eq(ProductSpec::getSpecName, productSpec.getSpecName()));
            if (count > 0) {
                throw new ServiceException("规格名称已存在");
            }
        } else {
            productSpec.setUpdateBy(SecurityUtils.getUsername());
        }
    }
}




